package com.homework.service;

import com.homework.dto.Result;
import com.homework.mapper.HomeworkRecordMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class AnalyticsService {

    private final HomeworkRecordMapper homeworkRecordMapper;

    /**
     * 获取开始时间趋势数据
     */
    public Result<List<Map<String, Object>>> getStartTimeTrend(Long studentId) {
        LocalDateTime endDate = LocalDateTime.now();
        LocalDateTime startDate = endDate.minusDays(30); // 最近30天

        log.info("查询开始时间趋势数据 - 学生ID: {}, 时间范围: {} 到 {}", studentId, startDate, endDate);

        List<Map<String, Object>> data = homeworkRecordMapper.getStartTimeTrend(studentId, startDate, endDate);
        log.info("找到 {} 条开始时间趋势记录", data.size());

        return Result.success(data);
    }

    /**
     * 获取完成时间趋势数据
     */
    public Result<List<Map<String, Object>>> getCompletionTimeTrend(Long studentId) {
        LocalDateTime endDate = LocalDateTime.now();
        LocalDateTime startDate = endDate.minusDays(30); // 最近30天

        log.info("查询完成时间趋势数据 - 学生ID: {}, 时间范围: {} 到 {}", studentId, startDate, endDate);

        List<Map<String, Object>> data = homeworkRecordMapper.getCompletionTimeTrend(studentId, startDate, endDate);
        log.info("找到 {} 条完成时间趋势记录", data.size());

        return Result.success(data);
    }

    /**
     * 获取月度日历数据
     */
    public Result<Map<String, Object>> getCalendarData(Long studentId, Integer year, Integer month) {
        log.info("查询日历数据 - 学生ID: {}, 年: {}, 月: {}", studentId, year, month);
        List<Map<String, Object>> calendarData = homeworkRecordMapper.getCalendarData(studentId, year, month);
        log.info("找到 {} 条日历数据记录", calendarData.size());

        // 打印每条记录的关键信息
        calendarData.forEach(record -> {
            log.info("日历记录 - 标题: {}, 日期: {}, 必须完成: {}, 已完成必须: {}, 状态: {}",
                record.get("title"), record.get("date"), record.get("total_required"),
                record.get("required_completed"), record.get("status"));
        });

        Map<String, Object> result = new HashMap<>();
        result.put("year", year);
        result.put("month", month);
        result.put("data", calendarData);

        return Result.success(result);
    }

    /**
     * 获取作业完成记录
     */
    public Result<List<Map<String, Object>>> getHomeworkRecords(Long studentId, Integer year, Integer month) {
        log.info("查询历史作业记录 - 学生ID: {}, 年: {}, 月: {}", studentId, year, month);
        List<Map<String, Object>> records = homeworkRecordMapper.getHomeworkRecords(studentId, year, month);
        log.info("找到 {} 条历史作业记录", records.size());

        // 打印每条记录的关键信息
        records.forEach(record -> {
            log.info("历史记录 - ID: {}, 标题: {}, 日期: {}, 状态: {}",
                record.get("id"), record.get("title"), record.get("date"), record.get("status"));
        });

        return Result.success(records);
    }
}